<?php
use yii\helpers\Html;
use yii\widgets\DetailView;
use yii\widgets\LinkPager;
use yii\helpers\Url;
$this->params['breadcrumbs'][] = $this->title;

?>

  <div class="goods-lists">
    <div class="main">
    <div class="content-wrap">

    <?php if ($pro_name): ?>
        <h1 class="tt0">
          <span class="inlineBlock">
            <em class="keyWords">“<font color="#FF6600"><?=Html::encode($pro_name)?></font>”</em>的搜索结果
          </span>
        </h1>
    <?php endif ?>

      <div class="tt0">
        <div class="inlineBlock level levelItem">
          <span value='<?=$lv['lv1']['id']?>'>
            <?=Html::encode($lv['lv1']['name']?$lv['lv1']['name']:'请选择一级分类')?>
          </span>
          <i class="iconfont icon-angledown"></i>
          <div class="itemPop">

          <?php foreach ($level_1 as $key => $value): ?>
            <em value = "<?=Html::encode($value['id'])?>"><?=Html::encode($value['name'])?></em>
          <?php endforeach ?>
            
          </div>
        </div>
        <i class="iconfont icon-right"></i>
        <div class="inlineBlock level levelItem">
          <span value='<?=$lv['lv2']['id']?>'>
            <?=Html::encode($lv['lv2']['name']?$lv['lv2']['name']:'请选择二级分类')?>
          </span>
          <i class="iconfont icon-angledown"></i>
          <div class="itemPop">
            <?php if ($lv['lv1']['id']): ?>
              <?php foreach ($lv['lv2']['list'] as $key => $value): ?>
                <a href="/product/index/?lv1=<?=$lv['lv1']['id']?>&lv2=<?=$value['id']?>"><em value="<?=$value['id']?>"><?=Html::encode($value['name'])?></em></a>
              <?php endforeach ?>
            <?php else: ?>
              <span style="margin-left:10px">请先选择一级分类</span>
            <?php endif ?>
          </div>
        </div>
        <i class="iconfont icon-right"></i>
       <!--  <div class="inlineBlock totalSelect">
          <span>品牌1，品牌2，品牌3</span>
          <i class="iconfont icon-cuowu" style="cursor:pointer"></i>
        </div> -->
        <div class="right results">共<em class="resultsNum"><?=Html::encode($product_sum)?></em>个相关商品</div>
      </div>
      <input type="hidden" id="url" value="<?= Url::toRoute(['product/index/']);?>"> 
      <div class="classify">
        <div class="classLevel oneLine"><span class="tt1">品牌</span>
          <span  id="brand_list">
            <?php foreach ($brand_list as $key => $value): ?>
              <a href="Javascript:void(0)">
                <em class="check-box"></em>
                <span value='<?=$value['id']?>' <?php if($brand_id==$value['id']){echo "class='slec_red'";}?>>
                <?=Html::encode($value['name'])?></span>
              </a>
            <?php endforeach ?>
          </span>
          <span class="selectBtns" value='0'>
            <!-- <span class="moreSelect inlineBlock">多选 <i class="fa fa-plus"></i></span> -->

            <?php if(count($brand_list)>8): ?>
              <span class="morelevel inlineBlock">更多 <i class="fa fa-angle-down"></i></span>
            <?php endif ?>

          </span>
          <span class="selectConfirm">
            <span class="confirm btn-small-default btn">确定</span>
            <span class="cancel btn-small-empty1 btn">取消</span>
          </span>
        </div>
        <div class="classLevel oneLine" style="display:none"><span class="tt1">类型</span>
            <span id="category_list3">
              <?php foreach ($category_list as $key => $value): ?>
                <a href="Javascript:void(0)">
                  <span value='<?=$value['id']?>' <?php if($cate_id==$value['id']){echo "class='slec_red'";}?>>
                  <?=Html::encode($value['name'])?></span>
                </a>
              <?php endforeach ?>
            </span>
          <?php if(count($category_list)>8): ?>
          <span class="selectBtns">
            <span class="morelevel inlineBlock" id="cate_more">更多<i class="fa fa-angle-down"></i></span>
          </span>
          <?php endif ?>
        </div>
        <div class="filterType">
          <ul class="navs">
            <li>综合 <i class="fa fa-arrow-down"></i></li>
            <li>销量 <i class="fa fa-arrow-up"></i></li>
            <li  class="current price">价格<i class="fa fa-arrow-up"></i></li>
          </ul>
          <!-- <span class="freeExample inlineBlock">
            <span class="check-box"></span>免费拿样
            <span class="tip-icon">?</span>
          </span> -->
          <!-- <span class="miniNumber inlineBlock">起订量 <input type="number" min="1" value="10">以下</span> -->
          <span class="page right combine_page">共&nbsp;<?=Html::encode($page_sum)?>&nbsp;页</span>

<?= LinkPager::widget([
      'pagination' => $pages,
      'maxButtonCount' => 5,
      'options' => ['class' => 'pagination page right'], 
      'nextPageLabel' => '<span class="next inlineBlock"><i class="fa fa-angle-right"></i></span>', 
      'prevPageLabel' => '<span class="pre inlineBlock"><i class="fa fa-angle-left"></i></span>', 
]); ?>          
          
        </div>
      </div>

      <div class="goodsItems">

      <?php if($product_list){?>
        <div class="items left">
          <?php foreach ($product_list as $k => $v): ?>
            <div class="item">
              <div class="img" id="img0">
                <a href="<?= Url::toRoute(['product/'.$v['id']]);?>">
                <img src="<?= Html::encode($v['img_url']?$v['img_url']:'http://static.tianpinwang.com.cn/v1/images/moment/goods.png');?>" alt=""></a>
                <div class ="addCartMaskdiv" value = "<?=Html::encode($v['id'])?>">
                  <div class="addCartMask"></div>
                  <p class="txtMask"><i class="fa fa-shopping-cart"></i>   加入购物车</p>
                </div>
              </div>
              
              <a class="name" value = '<?=Html::encode($v['name'])?>' href="<?= Url::toRoute(['product/'.$v['id']]);?>">
              <?=Html::encode((strlen($v['name'])>78)?mb_substr($v['name'],0,25,'utf-8').'...':$v['name'])?>
              </a>
              <p class="priceInfo">
                <span class="price">￥<em class="priceNum"><?= Html::encode($v['product_price'])?></em>/<?= Html::encode($v['price_unit'])?></span>
                <span class="right">已售<em><?= Html::encode($v['sale_sum']?$v['sale_sum']:0)?></em><?= Html::encode($v['price_unit'])?></span>
              </p>
              <div class="addCartSuccess"><i class="iconfont icon-success"></i>成功添加进货单</div>
            </div>
          <?php endforeach ?>
        </div>
      <?php } else{?>

        <div class="items left">

            <div class="noneGoods">
              <img src="http://static.tianpinwang.com.cn/v1/images/addCartFail.png" alt="">
              <p class="tt0">在您的筛选条件下，没找到符合的商品</p>
              <p class="txt0">您可以重新筛选</p>
              <p class="returnPre"><a href="<?= Url::toRoute(['product/']);?>" class="btn-middle-default">返回上一步</a></p>
            </div>

        </div>
      <?php }?>

        <div class="right historyLook">
          <h1 class="tt">最近浏览过</h1>
          <ul class="lookedList">

            <?php if (count($scan_list)): ?>

              <?php foreach ($scan_list as $k => $v): ?>
                  <li>
                  <a href="<?= Url::toRoute(['product/'.$v['id']]);?>" value='<?=Html::encode($v['name'])?>'>
                    <h1 class="name">
                    <?=Html::encode((strlen($v['name'])>24)?mb_substr($v['name'],0,7,'utf-8').'...':$v['name'])?>
                    </h1>
                    <p class="price">￥<?=Html::encode($v['product_price'])?>/<?=Html::encode($v['price_unit'])?></p>
                    <img src="<?=Html::encode($v['img_url']?$v['img_url']:'http://static.tianpinwang.com.cn/file/2016/07/20/65tyl65cnvnqxhpg.png')?>!s50x50" alt="" class="img" style = 'margin-top: 13px'>
                  </a>
                </li>
              <?php endforeach ?>
              
            <?php else: ?>
              <li class="nullInfo">暂无记录</li>
            <?php endif ?>
          </ul>
        </div>
      </div>


    </div>
  </div>


    <!--添加货品失败弹出框-->
    <div class="mask" style="display: none"></div>
    <div class="failToAdd" style="display: none;">
      <img src="http://static.tianpinwang.com.cn/v1/images/closeIcon1.png" alt="" class="close">
      <div class="txts">
        <i class="iconfont icon-info"></i>
        <h1 class="tt0">添加进货单失败</h1>
        <p class="txt0">已超出进货单最大容量</p>
      </div>
      <p class="btn"><a href="" class="btn-small-default">确定</a></p>
    </div>

    <!-- <div class="rangePop">
      <i class="iconfont icon-close1"></i>
      <span>最多可选5个品牌</span>
    </div> -->
  </div>

<style type="text/css">
  .pagination li{float: left;line-height: 25px;margin-left: 5px;} 
  .pagination li a{color: #999; }
  .pagination .active a{color:#31a900;}
  .combine_page{line-height: 25px}
  .fa-times{cursor: pointer;}
  .slec_red{color:#31A900 !important;}
</style>
<script>
  $(function(){

    //JS控制当前页面导航上面的active
    var cur_lv1 = <?=Yii::$app->request->get('lv1')?Yii::$app->request->get('lv1'):0?>;
    $('#lv1_nav a').attr('class','');
    if ( cur_lv1== 72) {
      $('#lv1_nav a').eq(1).attr('class','cur');
    }else if(cur_lv1 == 95){
      $('#lv1_nav a').eq(2).attr('class','cur');
    }else if(cur_lv1 == 96){
      $('#lv1_nav a').eq(3).attr('class','cur');
    }
    

    //高亮关键词搜索结果里面的关键词
    var search = <?php echo "'".$pro_name."'"?>;

    if(search){
      var aaa = $("a:contains("+search+")");
      for (var i = 0; i < aaa.length; i++) {
        var old = aaa.eq(i);
        old.html(old.html().replace(new RegExp(search,'g'), '<font color="#FF6600">'+search+'</font>'));
      }
    }

    //$("a:contains("+search+")").text();

    //点击品牌筛选那里的X，清空品牌筛选
    $('.icon-cuowu').click(function(){
      $('.totalSelect').attr('style','display:none;')
    });
//    layer.tips('在上面', '#img0', {
//      tips: 3
//    });


//    setTimeout(function(){layer.close(index3)},2000);



    $('.rangePop').css('display','block');

    setTimeout(function(){$('.rangePop').fadeOut(600)},1000);


    var $selectConfirm=$('.selectConfirm');
    var $selectBtns=$('.selectBtns');
    $('.morelevel').on('click',function(){
      if(!$(this).hasClass('flagClass')){
        $(this).addClass('flagClass').html('收起<i class="fa fa-angle-up"></i>').parent().parent().removeClass('oneLine');
      }else {
        $(this).removeClass('flagClass').html('更多<i class="fa fa-angle-down"></i>').parent().parent().addClass('oneLine');
      }
    });
    $('.moreSelect').on('click',function(){
      $(this).parent().parent().removeClass('oneLine');
      $(this).parent().siblings().find('.check-box').css('display','block');
      $(this).parent().css('display','none');
      $selectConfirm.css('display','block');
    });

    $selectConfirm.find('.cancel').on('click',function(){
      $(this).parent().siblings().find('.check-box').css('display','none');
      $(this).parent().parent().addClass('oneLine');
      $selectConfirm.css('display','none');
      $selectBtns.css('display','block');
    });

    //,.oneLine a
    $(document).on('click','.oneLine a .check-box',function(e){
      e.preventDefault();
    });


    var $goodsItems=$('.goodsItems');

    var $txtMask=$goodsItems.find('.txtMask');
    $txtMask.on('click',function(){
      var $successtip=$(this).parents().siblings('.addCartSuccess');
      $successtip.css('display','block');
      setTimeout(function(){$successtip.fadeOut(1000)},1000)
    });

    var $items=$goodsItems.find('.item');
    $items.on('mouseenter',function(){
      $(this).addClass('hover').siblings().removeClass('hover');
    }).on('mouseleave',function(){
      $(this).removeClass('hover');
    })

    $('.levelItem').on('mouseenter',function(){
      $(this).addClass('hover');
    }).on('mouseleave',function(){
      $(this).removeClass('hover');
    });

    })
  $(function(){

    //一级分类点击事件
    $('.itemPop:eq(0) em').click(function(){
      var lv1 = $(this);
      $('.levelItem:eq(0) span').attr('value',lv1.attr('value'));
      $('.levelItem:eq(0) span').text($(this).text());

      $.post('',{'level_1_id':lv1.attr('value')},function(data){
        var data = eval('('+data+')');
        var level2Str='';
        $('.levelItem:eq(1) span:eq(0)').text('请选择二级分类');
        var bid = <?php echo $brand_id?$brand_id:0;?>;

        for(var k in data){
          var href = $('#url').val()/*+'/?bid='+bid*/+'/?lv2='+data[k]['id'];

          if($('.levelItem:eq(0) span:eq(0)').attr('value')>0){
            href +='&lv1='+$('.levelItem:eq(0) span:eq(0)').attr('value');
          }/*
          if($('.levelItem:eq(1) span:eq(0)').attr('value')>0){
            href +='&lv2='+$('.levelItem:eq(1) span:eq(0)').attr('value');
          }*/

          level2Str+="<a href = '"+href+"'><em value = '"+data[k]['id']+"'>"+data[k]['name']+"</em></a>";
        }
        $('.itemPop:eq(1)').html(level2Str);
      });
    });

    //二级分类点击事件
    /*$('.itemPop:eq(1)').on('click','em',function(){
      var lv2 = $(this);
      $('.levelItem:eq(1) span').attr('value',lv2.attr('value'));
      $('.levelItem:eq(1) span').text($(this).text());

      $.post("",{'level_2_id':lv2.attr('value')},function(data){
        var data = eval('('+data+')');
        var level3Str='';
        if(parseInt(data.length)<9){
          $('#cate_more').attr('style','display:none');
        }

        for(var k in data){
          level3Str+="<a href='Javascript:void(0)'><span value="+data[k]['id']+">&nbsp;"+data[k]['name']+"&nbsp;</span>";
        }
        $('#category_list3').html(level3Str);
      });
    });*/

    //商品品牌点击
    $('#brand_list').on('click','span',function(){
      var bid = $(this).attr('value');
      var $url;
      var cate_id = <?php echo $cate_id?$cate_id:0;?>;
      /*if(cate_id>0 && bid>0){
        $url ='/?bid='+bid+'&cid='+cate_id;
      }else{*/
        $url ='/?bid='+bid;
      //}
      if($('.levelItem:eq(0) span:eq(0)').attr('value')>0){
        $url +='&lv1='+$('.levelItem:eq(0) span:eq(0)').attr('value');
      }
      if($('.levelItem:eq(1) span:eq(0)').attr('value')>0){
        $url +='&lv2='+$('.levelItem:eq(1) span:eq(0)').attr('value');
      }

      window.location.href = $('#url').val()+$url;
    })
    //3级分类点击
    /*$('#category_list3').on('click','span',function(){
      var cid = $(this).attr('value');
      var brand_id = <?php echo $brand_id?$brand_id:0;?>;
      var $url;
      if(brand_id>0 && cid>0){
        $url ='/?bid='+brand_id+'&lv2='+cid;
      }else{
        $url ='/?lv2='+cid;
      }
      if($('.levelItem:eq(0) span:eq(0)').attr('value')>0){
        $url +='&lv1='+$('.levelItem:eq(0) span:eq(0)').attr('value');
      }
      if($('.levelItem:eq(1) span:eq(0)').attr('value')>0){
        $url +='&lv2='+$('.levelItem:eq(1) span:eq(0)').attr('value');
      }
      window.location.href = $('#url').val()+$url;
    })*/

    //加入购物车
    $('.addCartMaskdiv').on('click',function(e){
      
      e.preventDefault();
      var product_num = 1;
      var product_id = $(this).attr('value');
      
      var uid = <?php echo Yii::$app->user->isGuest?1:0?>;
      if(uid){
        layer.msg('请先登录',{time:1200},function(){
          window.location.href = '/site/login';
        });
        return false;
      }
      $.ajax({
        type: "POST",
        url: "/shopping-cart/add",
        data: "product_id="+product_id+"&product_num="+product_num+"&_csrf=<?=Yii::$app->request->getCsrfToken()?>",
        dataType: "json",
        success: function(data){
          if(!data.errNo){
            $('#now_cart').html(data.nowProduct);
            $suc_tips = $(this).parents().siblings('.addCartSuccess');
            $suc_tips.css('display','block');
            setTimeout(function(){$suc_tips.fadeOut(1000)},1000);
            window.location.href = "";
          }else{
            layer.msg(data.msg,{time:1200});
          }
        }
      });
    })

  })/*
  $(function(){
    $("a:contains('...')").mouseover(function(){
      var th=this;
      layer.tips($(th).attr('value'),th,{tips:[1,'#31A900'],time: 2000});
    });
  })*/
</script>